Thread: [beginner] creating a simple address book...

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    21

    [beginner] creating a simple address book...

    I've decided to create a little mini project that I can do to learn / revise some things...

    I know that it won't be the best but the main point of this is to try and learn things that I have already come across, rather than employ lot's of functions that I've never seen (even if at the expense of the program...)

    So, my address book will be simple, and the thing's that I'm expecting to use in it are :

    Pointers, Linked Lists
    Malloc
    Structs
    Typedefs
    Makefile, header file
    Putting functions into different program files

    OK... Baby steps....

    I have started the program trying to create a struct, and getting it working with a couple of entries before going onto user input and splitting it up.


    here's the full code :

    Code:
        #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    
    
    int main(void)
    {
    
    
        // Struct type address book. Just a name, a number and a pointer
    
    
        struct addressBook {
        
            char * name;
            int number;
            struct addressBook *next;
        };
    
    
        struct addressBook *head = NULL;
        struct addressBook *addEntry0,*addEntry1, *addEntry2; 
    
    
        // create a few struct pointers to test out
        
        addEntry0 = (struct addressBook *) malloc(sizeof(struct addressBook));
        addEntry1 = (struct addressBook *) malloc(sizeof(struct addressBook));
        addEntry2 = (struct addressBook *) malloc(sizeof(struct addressBook));
    
    
        
        addEntry0->number = 12434;
        addEntry0->name = "Ben";
        addEntry0->next = addEntry1;
    
    
        
        addEntry1->number = 12344;
        addEntry1->name = "Sophia";
        addEntry1->next = addEntry2;
    
    
        addEntry2->number = 3333;
        addEntry2->name = "Jim";
        addEntry2->next = NULL;
    
    
        printf("%s\n", addEntry0->name);
        
        printf("%i\n", addEntry0->number);
    
    
          
        return 0;
    }

    I'm a bit lost at this point...

    My knee jerk thought is to create a for loop, and cycle through the list.

    I'm not sure how this would apply to this though? Without using the familiar type of print loop such as :

    Code:
    for (i = 0; ;i++)
    {
           printf("%i\n", array[i];
    }
    I'm thinking that I need to create a temporary struct that can be used to assign the value of the next struct in the list, and then somehow print from that....

    I'll try and write the logic out :

    while temp != NULL (The last node value is assigned NULL to show us where the end of the list is)

    create a temporary pointer that can be used to keep track of where we are in the list.

    print out the current entry name and number

    then assign the temp pointer value to the * next of the current struct. So if we are in entry1 the *next should be the address of entry 2.

    Print out entry 2 name and number, assign entry 2 next to the temp value.



    Any help much appreciated...
    cheers!

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    You do sort of create a temp struct, but it doesn't work like an array, it works like :

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    struct addressBook {
        char * name;
        int number;
        struct addressBook *next;
        struct addressBook *prev;
    };
    void link(struct addressBook *linked,struct addressBook *next, struct addressBook *prev);
    int main(void)
    {
        struct addressBook *addEntry0,*addEntry1, *addEntry2;
        struct addressBook *temp;
    
        addEntry0 = (struct addressBook *) malloc(sizeof(struct addressBook));
        addEntry1 = (struct addressBook *) malloc(sizeof(struct addressBook));
        addEntry2 = (struct addressBook *) malloc(sizeof(struct addressBook));
    
        addEntry0->number = 12434;
        addEntry0->name = "Ben";
        addEntry0->prev = NULL;
    
        addEntry1->number = 12344;
        addEntry1->name = "Sophia";
    
        addEntry2->number = 3333;
        addEntry2->name = "Jim";
        addEntry2->next = NULL;
    
        link(addEntry1, addEntry2, addEntry0);
    
        printf("----Forward----\n");
        for(temp = addEntry0; temp != NULL; temp = temp->next)
            printf("Name: %s\n #%d\n\n", temp->name, temp->number);
    
        printf("----Backward----\n");
        for(temp = addEntry2; temp != NULL; temp = temp->prev)
            printf("Name: %s\n #%d\n\n", temp->name, temp->number);
    
        return 0;
    }
    
    void link(struct addressBook *linked,struct addressBook *next, struct addressBook *prev)
    {
        linked->prev = prev;   ///mid prev is first
        linked->next = next;   ///mid next is last
        prev->next = linked;   ///first next is mid
        next->prev = linked;   ///last prev is mid
    }
    Where temp is a struct pointer used to walk down the list of addresses. Also I added a linker, so took the struct out of the main function. It's not necessary to make a link function though.
    Last edited by Alpo; 05-11-2014 at 03:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. address book
    By cboard in forum C Programming
    Replies: 10
    Last Post: 04-05-2007, 11:47 PM
  2. Address Book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-10-2002, 05:18 PM
  3. address book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-01-2002, 08:19 PM
  4. Address Book
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 09-09-2002, 01:02 PM
  5. Address book
    By sundeeptuteja in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2002, 02:40 PM